import 'package:flutter/material.dart';
import 'package:flutter_chat_ui/pages/onnx/input_widgets/onnx_text_input_view.dart';
import 'package:flutter_chat_ui/pages/onnx/input_widgets/onnx_voice_button_view.dart';

import 'onnx_talk_bar_tools_view.dart';

///
/// 文本输入框/按住说话视图
///
class OnnxCenterAreaView extends StatelessWidget {
  final ValueNotifier<ChatInputMode> inputModeNotifier;
  final TextEditingController textEditingController;
  final FocusNode focusNode;

  /// 事件
  final VoidCallback? onSubmit;
  final VoidCallback? onStartVoiceRecord;
  final VoidCallback? onEndVoiceRecord;

  const OnnxCenterAreaView({
    super.key,
    required this.inputModeNotifier,
    required this.textEditingController,
    required this.focusNode,
    this.onSubmit,
    this.onStartVoiceRecord,
    this.onEndVoiceRecord,
  });

  @override
  Widget build(BuildContext context) {
    return AnimatedSwitcher(
      duration: const Duration(milliseconds: 150),
      layoutBuilder: (Widget? currentChild, List<Widget> previousChildren) {
        return Stack(
          alignment: Alignment.bottomCenter,
          children: <Widget>[
            ...previousChildren,
            if (currentChild != null) currentChild,
          ],
        );
      },
      transitionBuilder: (Widget child, Animation<double> animation) {
        return FadeTransition(opacity: animation, child: child);
      },
      child: ValueListenableBuilder<ChatInputMode>(
        valueListenable: inputModeNotifier,
        builder: (context, value, child) {
          if (value == ChatInputMode.keyboard) {
            return OnnxTextInputView(
              controller: textEditingController,
              focusNode: focusNode,
              hintText: '请输入您的问题...',
              onSubmitted: (value) => onSubmit?.call(),
            );
          } else {
            return const OnnxVoiceButtonView();
          }
        },
      ),
    );
  }
}
